package com.xgg.taiwuinfo.filter;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Spinner;

import com.xgg.taiwuinfo.R;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

abstract public class FC_EqualSelect extends FilterComponent{
    protected int ReadValue;
    protected int TempValue;
    protected int ReadCompare;
    protected int TempCompare;
    protected List<Map<String, String>> mValueMap;
    protected List<Map<String, String>> mCompareMap;

    public FC_EqualSelect()
    {
        value_listener = new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapter, View view, int position, long id) {
                TempValue = (int)id;
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {}
        };

        compare_listener = new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long id) {
                TempCompare = (int)id;
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        };

        mCompareMap = new ArrayList<>();
        AddMap(mCompareMap , "是" , FC_EQUAL + "");
        AddMap(mCompareMap , "不是" , FC_NOTEQUAL + "");
    }

    @Override
    final public int getItemLayout(){ return R.layout.item_fc_compareselect; }

    @Override
    final public void save()
    {
        if (TempValue != ReadValue)
        {
            ReadValue = TempValue;
        }
        ReadCompare = TempCompare;
    }

    @Override
    final public JSONObject getJsonObj()
    {
        JSONObject obj = new JSONObject();
        try
        {
            obj.put("id" , mTypeId);
            obj.put("c" , ReadCompare);
            obj.put("v" , ReadValue);
        }
        catch (Exception e)
        {
            Log.d("SaveJson" , e.getLocalizedMessage());
        }
        return obj;
    }

    @Override
    final public void selectValue(Spinner spinner)
    {
        int itype = TempValue;
        for (int i = 0; i < mValueMap.size(); i++)
        {
            int type = Integer.parseInt(mValueMap.get(i).get("v"));
            if (itype == type)
            {
                spinner.setSelection(i);
                return;
            }
        }
    }

    @Override
    final public void selectCompare(Spinner spinner)
    {
        int itype = TempCompare;
        for (int i = 0; i < mCompareMap.size(); i++)
        {
            int type = Integer.parseInt(mCompareMap.get(i).get("v"));
            if (itype == type)
            {
                spinner.setSelection(i);
                return;
            }
        }
    }

    @Override
    final public List<Map<String, String>> getCompareString(Context ctx){
        return mCompareMap;
    }

    @Override
    final public void discard(){
        TempCompare = ReadCompare;
        TempValue = ReadValue;
    }

    @Override
    final public boolean isEdited()
    {
        return (ReadValue != TempValue) || (ReadCompare != TempCompare) ;
    }

    @Override
    final public String getDesc()
    {
        for (int i = 0; i < mValueMap.size(); i++)
        {
            Map<String,String> map = mValueMap.get(i);
            if (ReadValue == Integer.parseInt(map.get("v"))){
                ValueStr = map.get("f");
                break;
            }
        }
        for (int i = 0; i < mCompareMap.size(); i++)
        {
            Map<String,String> map = mCompareMap.get(i);
            if (ReadCompare == Integer.parseInt(map.get("v"))){
                CompStr = map.get("f");
                break;
            }
        }
        return String.format("%s %s %s\r\n" , FieldStr , CompStr , ValueStr);
    }
}
